<?php
/**
* A template editor for use within a browser interface. Ideal for letting
* users alter or create templates directly within the admin interface.
*
* @package Scribble
* @subpackage Plugin
*/
use Buan\AutoLoader;
use Buan\Config;
use Buan\ExtensionManager;
use Buan\UrlCommand;
class SpTemplateEditor extends ScribblePlugin implements IScribblePlugin {

	/**
	* Setup a template language to make it available as a choice in the template
	* editor.
	*
	* The $genEngine argument will be passed to "call_user_func()" so it can be a
	* global function name, or class+method array. When called it must return an
	* instance of a class that implements Buan\IViewEngine.
	*
	* @examples
	* $plugin->addTemplateLanguage('php', 'PHP', 'createPhpEngineInstance');
	* $plugin->addTemplateLanguage('smarty', 'Smarty', array('MyClass', 'AMethod'));
	*
	* @param string Unique name for this template (lowercase-hyphenated)
	* @param string Human-readable label for this template
	* @param mixed Name of function/method that will generate the engine instance
	*/
	public function addTemplateLanguage($name, $label, $genEngine) {

		// Add to list of options
		$templateLanguages = Config::get('ext.Scribble.plugin.SpTemplateEditor.templateLanguages');
		$templateLanguages[$name] = array(
			'name'=>$name,
			'label'=>$label,
			'genEngine'=>$genEngine
		);
		Config::set('ext.Scribble.plugin.SpTemplateEditor.templateLanguages', $templateLanguages);
	}

	/**
	* Environment-agnostic configuration.
	*
	* @return void
	*/
	public function configure() {

		// Configuration
		Config::set('ext.Scribble.plugin.SpTemplateEditor.dir.views', dirname(__FILE__).'/views');
		Config::set('ext.Scribble.plugin.SpTemplateEditor.dir.resources', dirname(__FILE__).'/resources');
		Config::set('ext.Scribble.plugin.SpTemplateEditor.templateLanguages', array());

		// Add classpaths
		AutoLoader::addClassPath(dirname(__FILE__).'/lib');

		// Setup controllers
		$this->setupControllers(dirname(__FILE__).'/controllers',
			array("/sp-template-editor", ScribbleEnvironment::ADMIN)
		);

		// Define new privileges
		ScribbleAuth::definePrivilege('Content', 'template-editor', 'Template Editor', 'Grant ability to edit/create templates');
	}

	/**
	* Configure ADMIN environment.
	*
	* @return void
	*/
	private function configureAdmin() {

		// Add admin menus
		$ext = ExtensionManager::get('Scribble');
		$ext->addAdminMenuTemplate(dirname(__FILE__).'/admin-menus.tpl.php');

		// UI injection:
		// Template-selection UI ,links
		ScribbleEventManager::addEventListener('ui-inject:/scribble/page/select-template/secondary-workspace/links', function($event, $views) {

			// Add a view to the $views array
			if(ScribbleAuth::authorize(ScribbleSession::getUser(), 'template-editor')) {
				$v = new ScribbleAdminView(new Buan\StringViewEngine());
				$v->setSource('<a class="task task-create" href="'.$v->helpers->html->eUrl(UrlCommand::createUrl('sp-template-editor', 'create'), FALSE).'">Create A Template</a>');
				$views[] = $v;
			}
		});

		// UI injection:
		// Add a link to this plugin to the secondary workspace in the
		// template-selection UI
		ScribbleEventManager::addEventListener('ui-inject:/scribble/page/select-template/secondary-workspace', function($event, $views) {

			// Add a view to the $views array
			if(ScribbleAuth::authorize(ScribbleSession::getUser(), 'template-editor')) {
				$v = new ScribbleAdminView(new Buan\StringViewEngine());
				$v->setSource('<div class="hr"><hr /></div><p>If none of the templates here match your needs, why not use the <a href="'.$v->helpers->html->eUrl(UrlCommand::createUrl('sp-template-editor'), FALSE).'">template editor plugin</a> to generate a new one.</p>');
				$views[] = $v;
			}
		});
	}

	/**
	* Environment-specific configuration.
	*
	* @return void
	*/
	public function configureEnvironment($e) {
		if($e===ScribbleEnvironment::ADMIN) {
			return $this->configureAdmin();
		}
		else {
			return $this->configureFrontend();
		}
	}

	/**
	* Configure FRONTEND environment.
	*
	* @return void
	*/
	private function configureFrontend() {
	}

	/**
	* Return information URL.
	*
	* @return string
	*/
	public function getInformationUrl() {
		return "http://www.scribblecms.co.uk/plugins/sp-template-editor";
	}

	/**
	* Patch.
	*
	* @param string
	* @param string
	* @return void
	*/
	public function patchVersion($to, $from) {
	}

	/**
	* Test version compatibility.
	*
	* @param string
	* @param string
	* @return bool
	*/
	public function testVersionCompatibility($to, $from) {
		$to = (float)$to;
		$from = (float)$from;
		if($to<=0.1) {
			return TRUE;
		}
		return FALSE;
	}
}
?>
